package bb.lanxing.manager;

import com.baidu.mapapi.model.LatLng;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import bb.lanxing.App;
import bb.lanxing.common.config.Constants;
import bb.lanxing.device.sgdevcie.xpair.uimodel.FailedRemoveBondDevices;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.model.map.GeoPoint;
import bb.lanxing.util.ui.GsonUtils;

public class SharedManager extends BaseSharedPreference {
    public static final String KEY_ANT_PLUS_IS_ENABLED = "key_ant_plus_is_enabled";
    public static final String KEY_CREATE_ROUTEBOOK_DOWNLOADED = "KEY_CREATE_ROUTEBOOK_DOWNLOADED";
    public static final String KEY_CREATE_ROUTEBOOK_MINE = "KEY_CREATE_ROUTEBOOK_MINE";
    public static final String KEY_DUMP_LOG_AFTER_STOP_SPORT = "key_dump_log_after_stop_sport";
    public static final String KEY_IS_FW_TEST_MODE = "key_fw_test_mode";
    public static final String KEY_LASTET_PROVINCE_ID = "last_province_id";
    public static final String KEY_MAIN_CLUB_AUTH = "key_main_club_auth";
    public static final String KEY_PERSONALIZED_PUSH = "KEY_personalized_push";
    public static final String KEY_SERVERS_SUB_SYS_TIME = "servers_sub_system_time";
    public static final String KEY_SHOW_SPRINT_PROMPT_FIRST = "key_show_sprint_prompt";
    public static final String KEY_SPORT_SETTING_BIG_FONT_MODE = "key_big_font_mode";
    public static final String KEY_SPORT_SETTING_DISPLAY_MODE = "key_display_mode";
    public static final String KEY_SPORT_SETTING_NIGHT_MODE = "key_night_mode";
    public static final String KEY_USER_AGREEMENT = "KEY_USER_AGREEMENT_";
    public static final String KEY_USER_SETTING_AUTO_UPLOAD_MODE = "auto_upload_mode";
    public static final String KEY_USER_SETTING_SYNC_QQ_HEALTH = "sync_qq_health";
    public static final String KEY_USE_HTTPS = "KEY_USE_HTTPS";
    public static final String KEY_SHOW_EXPERIMENTAL = "KEY_SHOW_EXPERIMENTAL";
    public static final int LOGIN_STYLE_OTHER = -1;
    public static final int LOGIN_STYLE_QQ = 0;
    public static final int LOGIN_STYLE_WECHAT = 1;
    public static final int LOGIN_STYLE_WEIBO = 2;
    public static final String LOGOUT_SEND_MSG_TIMESTAMP = "LOGOUT_SEND_MSG_TIMESTAMP";
    public static final String KEY_TOKEN = "token";
    final private static String TAG = "SharedManager";
    private static final String KEY_GOOGLE_HOST = "googleHost";
    private static final String KEY_MINE_SPORT_TYPE = "mine_sport_type";
    private static final String KEY_PERSONAL_CADENCE = "personal_cadence";
    private static final String KEY_POWER_SAVING_MODE_TOAST = "powerSavingModeToast";
    private static final String KEY_RANK_PEOPLE_TYPE = "rank_time_type";
    private static final String KEY_TRANS_LOG = "trans_log";
    private static final String KEY_USER_PASSWORD = "password";
    private static final String KEY_USER_PHONE = "userPhone";
    private static final String KEY_VERSION_CLICK = "version_click";
    private static final String KEY_VERSION_NAME = "version_name";
    private static final String VALUE_LATITUDE_DEFAULT = "39.90280765527851";
    private static final String VALUE_LONGITUDE_DEFAULT = "116.40115274305667";
    private static String KEY_LOGIN_STYLE = "login_style";
    private static SharedManager instance;
    private boolean isEnablePersonalizedPush;
    private boolean isTransLog;
    private boolean isUseHttps;
    private boolean showExperimental;

    private SharedManager() {
        super(PreferenceManager.getDefaultSharedPreferences(App.getContext()));
        this.isUseHttps = true;
        this.isEnablePersonalizedPush = true;
        this.isTransLog = false;
        getSettingsFromPreferences();
    }

    public static void refresh() {
        instance = new SharedManager();
    }

    public static SharedManager getInstance() {
        if (instance == null) {
            instance = new SharedManager();
        }
        return instance;
    }

    private String getTabRedDotKey(int type) {
        return switch (type) {
        case 1 -> SPConstant.KEY_SPORT_REDDOT_COUNT;
        case 2 -> SPConstant.KEY_MINE_REDDOT_COUNT;
        case 3 -> SPConstant.KEY_DISCOVERY_REDDOT_COUNT;
        case 4 -> SPConstant.KEY_LUSHU_REDDOT_COUNT;
        case 5 -> SPConstant.KEY_CLUB_REDDOT_COUNT;
        default -> null;
        };
    }

    @Override
    public void commit() {
        super.commit();
    }

    @Override
    public boolean contains(String str) {
        return super.contains(str);
    }

    @Override
    public boolean getBoolean(String str, boolean z) {
        return super.getBoolean(str, z);
    }

    @Override
    public Enum getEnum(String str, Enum r2) {
        return super.getEnum(str, r2);
    }

    @Override
    public float getFloat(String str, float f) {
        return super.getFloat(str, f);
    }

    @Override
    public int getInt(String str, int i) {
        return super.getInt(str, i);
    }

    @Override
    public long getLong(String key, long defVal) {
        return super.getLong(key, defVal);
    }

    @Override
    public String getString(String key, String defVal) {
        return super.getString(key, defVal);
    }

    @Override
    public void registerOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        super.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }

    @Override
    public void remove(String str) {
        super.remove(str);
    }

    @Override
    public void setValue(String str, Object obj) {
        super.setValue(str, obj);
    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        super.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }

    private void getSettingsFromPreferences() {
        this.isUseHttps = getUseHttpsFromPreference();
        this.isTransLog = getTransLogFromPreference();
        this.showExperimental = getShowExperimentalFromPreference();
        this.isEnablePersonalizedPush = isEnablePersonalizedPushFromPF();
    }

    public Long getLogoutTime(String phoneNum) {
        return getLong(LOGOUT_SEND_MSG_TIMESTAMP + phoneNum, 0L);
    }

    public int getCreateRouteBookDownloadIsNeedRefresh() {
        return getInt(KEY_CREATE_ROUTEBOOK_DOWNLOADED, 0);
    }

    public void setCreateRouteBookDownloadIsNeedRefresh(int i) {
        setValue(KEY_CREATE_ROUTEBOOK_DOWNLOADED, i);
    }

    public void setCreateRouteMineIsNeedRefresh(int i) {
        setValue(KEY_CREATE_ROUTEBOOK_MINE, i);
    }

    public void setTabRedDotCount(int type, int dotNum) {
        setValue(getTabRedDotKey(type), dotNum);
    }

    public int getDisplayMode() {
        return getInt(KEY_SPORT_SETTING_DISPLAY_MODE, 2);
    }

    public void setDisplayMode(int i) {
        setValue(KEY_SPORT_SETTING_DISPLAY_MODE, i);
    }

    public boolean isPowerSavingModeToast() {
        return getBoolean(KEY_POWER_SAVING_MODE_TOAST, false);
    }

    public void setPowerSavingModeToast(boolean z) {
        setValue(KEY_POWER_SAVING_MODE_TOAST, z);
    }

    public boolean isSyncQQHealth() {
        return getBoolean(KEY_USER_SETTING_SYNC_QQ_HEALTH, false);
    }

    public void setSyncQQHealth(boolean z) {
        setValue(KEY_USER_SETTING_SYNC_QQ_HEALTH, z);
    }

    public int getAutoUploadMode() {
        return getInt(KEY_USER_SETTING_AUTO_UPLOAD_MODE, 1);
    }

    public void setAutoUploadMode(int i) {
        setValue(KEY_USER_SETTING_AUTO_UPLOAD_MODE, i);
    }

    public String getGoogleHost() {
        return getString(KEY_GOOGLE_HOST, Constants.DEFAULT_GOOGLE_HOST);
    }

    public void setGoogleHost(String googleHost) {
        setValue(KEY_GOOGLE_HOST, googleHost);
    }

    public boolean isNightMode() {
        return getBoolean(KEY_SPORT_SETTING_NIGHT_MODE, false);
    }

    public void setNightMode(boolean z) {
        setValue(KEY_SPORT_SETTING_NIGHT_MODE, z);
    }

    public boolean isBigFontMode() {
        return getBoolean(KEY_SPORT_SETTING_BIG_FONT_MODE, false);
    }

    public void setBigFontMode(boolean z) {
        setValue(KEY_SPORT_SETTING_BIG_FONT_MODE, z);
    }

    public String getUsername() {
        return getString("username", null);
    }

    public void setUsername(String username) {
        setValue("username", username);
    }

    public boolean isSignoutFlag() {
        return RemoteSharedPreference.getInstance().getBoolean(SPConstant.KEY_IS_SIGN_OUT, true);
    }

    public void setSignoutFlag(boolean signoutFlag) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_IS_SIGN_OUT, signoutFlag);
    }

    public int getPersonalCadence() {
        return getInt(KEY_PERSONAL_CADENCE, 80);
    }

    public void setPersonalCadence(int i) {
        setValue(KEY_PERSONAL_CADENCE, i);
    }

    public boolean isUseHttps() {
        return this.isUseHttps;
    }

    public void setUseHttps(Boolean isUse) {
        this.isUseHttps = isUse;
        setValue(KEY_USE_HTTPS, isUse);
    }

    public boolean getUseHttpsFromPreference() {
        return getBoolean(KEY_USE_HTTPS, true);
    }

    public boolean isShowExperimental() {
        return this.showExperimental;
    }

    public void setShowExperimental(Boolean showExperimental) {
        this.showExperimental = showExperimental;
        setValue(KEY_SHOW_EXPERIMENTAL, showExperimental);
    }

    public boolean getShowExperimentalFromPreference() {
        return getBoolean(KEY_SHOW_EXPERIMENTAL, true);
    }

    public boolean isEnablePersonalizedPush() {
        return this.isEnablePersonalizedPush;
    }

    public boolean isEnablePersonalizedPushFromPF() {
        return getBoolean(KEY_PERSONALIZED_PUSH, true);
    }

    public void setPersonalizedPush(Boolean enable) {
        this.isEnablePersonalizedPush = enable;
        setValue(KEY_PERSONALIZED_PUSH, enable);
    }

    public boolean getIsTransLog() {
        return this.isTransLog;
    }

    public void setTransLog(boolean transLog) {
        this.isTransLog = transLog;
        setValue(KEY_TRANS_LOG, Boolean.valueOf(transLog));
    }

    public boolean getTransLogFromPreference() {
        return getBoolean(KEY_TRANS_LOG, false);
    }

    public int getVersionClick() {
        return getInt(KEY_VERSION_CLICK, 0);
    }

    public void setVersionClick(int versionClick) {
        setValue(KEY_VERSION_CLICK, versionClick);
    }

    public String getVersionName() {
        return getString(KEY_VERSION_NAME, "");
    }

    public void setVersionName(String versionName) {
        setValue(KEY_VERSION_NAME, versionName);
    }

    public int getMineSportType() {
        return getInt(KEY_MINE_SPORT_TYPE, 3);
    }

    public void setMineSportType(int type) {
        setValue(KEY_MINE_SPORT_TYPE, type);
    }

    public int getRankPeopleType() {
        return getInt(KEY_RANK_PEOPLE_TYPE, 0);
    }

    public void setRankPeopleType(int rankPeopleType) {
        setValue(KEY_RANK_PEOPLE_TYPE, rankPeopleType);
    }

    public int getEventId() {
        return getInt("eventId", 0);
    }

    public void setEventId(int eventId) {
        setValue("eventId", eventId);
    }

    public boolean isFwTestMode() {
        return getBoolean(KEY_IS_FW_TEST_MODE, false);
    }

    public LatLng getCurLatLngWithMP() {
        String tempLatitude =
                RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CUR_LATITUDE, VALUE_LATITUDE_DEFAULT);
        String tempLongitude =
                RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CUR_LONGITUDE, VALUE_LONGITUDE_DEFAULT);
        return new LatLng(Double.parseDouble(tempLatitude), Double.parseDouble(tempLongitude));
    }

    public IGeoPoint getCurIGeoPointWithMP() {
        String tempLatitude =
                RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CUR_LATITUDE, VALUE_LATITUDE_DEFAULT);
        String tempLongitude =
                RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CUR_LONGITUDE, VALUE_LONGITUDE_DEFAULT);
        return new GeoPoint(IGeoPoint.TYPE_EARTH, Double.parseDouble(tempLatitude), Double.parseDouble(tempLongitude));
    }

    public String getCurCityWithMP() {
        return RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CUR_CITY, null);
    }

    public void setCurLocationInfoWithMP(double latitude, double longitude, String city, String province) {
        Log.d(TAG, "setCurLocationInfoWithMP: " + latitude + ", " + longitude + ", " + city + ", " + province);
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_CUR_LATITUDE, String.valueOf(latitude));
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_CUR_LONGITUDE, String.valueOf(longitude));
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_CUR_CITY, city);
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_CUR_PROVINCE, province);
    }

    public void setCurLocationInfoWithMP(double latitude, double longitude, String city) {
        setCurLocationInfoWithMP(latitude, longitude, city, null);
    }

    public int getSelfLocationUploadIntervalWithMP() {
        return RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SELF_LOCATION_UPLOAD_INTERVAL, -1);
    }

    public int getBaiduLocationIntervalWithMP() {
        return RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_BAIDU_LOCATION_INTERVAL, -1);
    }

    public int getGpsLocationIntervalWithMP() {
        return RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_GPS_LOCATION_INTERVAL, 1000);
    }

    public boolean isBiCiAutoConnectWithMP() {
        return RemoteSharedPreference.getInstance().getBoolean(SPConstant.KEY_BICI_AUTO_CONNECT, true);
    }

    public void setDeviceSpeedAlert(int i) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_SPEED_ALERT, i);
    }

    public String getHeartrateSectionWithMP() {
        return RemoteSharedPreference.getInstance().getString(SPConstant.KEY_HEARTRATE_SECTION, null);
    }

    public void setHeartrateSectionWithMP(String strSection) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_HEARTRATE_SECTION, strSection);
    }

    public void setPowerSectionWithMP(String strSection) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_POWER_SECTION, strSection);
    }

    public String getCadenceSectionWithMP() {
        return RemoteSharedPreference.getInstance().getString(SPConstant.KEY_CADENCE_SECTION, null);
    }

    public void setCadenceSectionWithMP(String strSection) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_CADENCE_SECTION, strSection);
    }

    public int getLastLoginStyle() {
        return getInt(KEY_LOGIN_STYLE, LOGIN_STYLE_OTHER);
    }

    public void setLoginStyle(int i) {
        setValue(KEY_LOGIN_STYLE, i);
    }

    public String getEmail() {
        return getString("email", "");
    }

    public void setEmail(String email) {
        setValue("email", email);
    }

    public void setPassword(String password) {
        setValue(KEY_USER_PASSWORD, password);
    }

    public int getUserId() {
        return RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_USER_ID, 0);
    }

    public void setUserId(int i) {
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_USER_ID, i);
    }

    public String getUserPhone() {
        return getString(KEY_USER_PHONE, "");
    }

    public void setUserPhone(String userPhone) {
        setValue(KEY_USER_PHONE, userPhone);
    }

    public long getServersAndSysTimeSub() {
        return getLong(KEY_SERVERS_SUB_SYS_TIME, 0L);
    }

    public void setServersAndSysTimeSub(long j) {
        setValue(KEY_SERVERS_SUB_SYS_TIME, j);
    }

    public boolean isUseAgreementAgree(int uid) {
        return getBoolean(KEY_USER_AGREEMENT + uid, false);
    }

    public boolean isUseAgreementAgree() {
        return getBoolean(KEY_USER_AGREEMENT, false);
    }

    public void setUserAgreement(boolean z) {
        setValue(KEY_USER_AGREEMENT, z);
    }

    public void setUserAgreement(int i, boolean z) {
        setValue(KEY_USER_AGREEMENT + i, z);
    }

    public String getLatlngValue() {
        return getString("key_latlnt_source", "0");
    }

    public void setLatlngValue(String value) {
        setValue("key_latlnt_source", value);
    }

    public void removeRemoveFailedDevice(String address) {
        FailedRemoveBondDevices removeFailedDevices = getRemoveFailedDevices();
        removeFailedDevices.removeOne(address);
        saveRemoveFailedDevices(removeFailedDevices);
    }

    public void addRemoveFailedDevice(String address) {
        FailedRemoveBondDevices removeFailedDevices = getRemoveFailedDevices();
        removeFailedDevices.addOne(address);
        saveRemoveFailedDevices(removeFailedDevices);
    }

    public FailedRemoveBondDevices getRemoveFailedDevices() {
        try {
            FailedRemoveBondDevices failedRemoveBondDevices = GsonUtils.Companion.getInstance().fromJson(
                    RemoteSharedPreference.getInstance().getString(SPConstant.KEY_FAILED_REMOVE_DEVICES, ""),
                    FailedRemoveBondDevices.class);
            return failedRemoveBondDevices != null ? failedRemoveBondDevices : new FailedRemoveBondDevices();
        } catch (Exception e) {
            e.printStackTrace();
            return new FailedRemoveBondDevices();
        }
    }

    public void saveRemoveFailedDevices(FailedRemoveBondDevices failedRemoveBondDevices) {
        try {
            RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_FAILED_REMOVE_DEVICES,
                    GsonUtils.Companion.getInstance().toJson(failedRemoveBondDevices));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isThisDeviceFirstConnect(String address) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        return remoteSharedPreference.getBoolean(SPConstant.KEY_IS_FIRST_SHOWN + address, true);
    }

    public void setThisDeviceFirstConnect(String address, boolean isFirst) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        remoteSharedPreference.setValue(SPConstant.KEY_IS_FIRST_SHOWN + address, isFirst);
    }

    public boolean isThisDeviceAutoSync(String address, boolean defaultValue) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        return remoteSharedPreference.getBoolean(SPConstant.KEY_IS_DEVICE_AUTO_SYNC + address, defaultValue);
    }

    public void setThisDeviceAutoSync(String address, boolean autoSync) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        remoteSharedPreference.setValue(SPConstant.KEY_IS_DEVICE_AUTO_SYNC + address, Boolean.valueOf(autoSync));
    }

    public void setIsFirstRun(boolean isFirstRun) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        remoteSharedPreference.setValue(SPConstant.KEY_IS_FIRST_RUN, isFirstRun);
    }

    public boolean isFirstRun() {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        return remoteSharedPreference.getBoolean(SPConstant.KEY_IS_FIRST_RUN, true);
    }

    public int getSportType() {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        return remoteSharedPreference.getInt(SPConstant.SPORT_TYPE, 0);
    }

    public void setSportType(int sportType) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        remoteSharedPreference.setValue(SPConstant.SPORT_TYPE, sportType);
    }

    public void saveCurConnDeviceAddress(String deviceAddress) {
        Log.d(TAG, " the device address is:" + deviceAddress);
        setValue(SPConstant.DEVICE_ADDRESS, deviceAddress);
    }

    public String getCurConnDeviceAddress() {
        return getString(SPConstant.DEVICE_ADDRESS, null);
    }

    public void setTimeAutoSync(String address, int autoSync) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        remoteSharedPreference.setValue(SPConstant.KEY_IS_TIME_AUTO_SYNC + address, autoSync);
    }

    public int getTimeAutoSync(String address, int defaultValue) {
        RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
        return remoteSharedPreference.getInt(SPConstant.KEY_IS_TIME_AUTO_SYNC + address, defaultValue);
    }

}
